package com.edu.internship.busi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.internship.busi.dto.DictionaryDTO;
import com.edu.internship.busi.entity.Dictionary;
import com.edu.internship.busi.service.IDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * code is far away from bug with the animal protecting
 *
 * @author lishiquan
 * @description <p>Dictionary 的controller</p>
 * @date 2020-07-03
 */
@RestController
@RequestMapping("api/dictionary")
@Slf4j
@Api(tags = {"字典的控制器"})
public class DictionaryController {

    @Resource
    private IDictionaryService dictionaryService;

    @GetMapping
    @ApiOperation(value = "分页查询-Dictionary", notes = "根据条件进行查询-Dictionary")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页数", dataTypeClass = Long.class, paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页几条记录", dataTypeClass = Long.class, paramType = "query", defaultValue = "10")
    })
    public IPage<Dictionary> page(DictionaryDTO dto, @RequestParam(defaultValue = "1") Long current,
                                  @RequestParam(defaultValue = "10") Long size) {
        LambdaQueryWrapper<Dictionary> dictionaryQw = Wrappers.lambdaQuery();
        dictionaryQw.like(StringUtils.isNotBlank(dto.getDictNameEn()), Dictionary::getDictNameEn, dto.getDictNameEn())
                .like(StringUtils.isNotBlank(dto.getDictNameCn()), Dictionary::getDictNameCn, dto.getDictNameCn())
                .orderByDesc(Dictionary::getCreateTime);
        return dictionaryService.page(new Page<>(current, size), dictionaryQw);
    }

    @PostMapping("create")
    @ApiOperation(value = "新增-Dictionary", notes = "新增-Dictionary")
    public void create(@Validated @RequestBody DictionaryDTO dto) {
        dictionaryService.createDictionary(dto);
    }

    @PostMapping("update")
    @ApiOperation(value = "更新-Dictionary", notes = "更新-Dictionary")
    public void update(@Validated @RequestBody DictionaryDTO dto) {
        dictionaryService.updateDictionary(dto);
    }

    @PostMapping("remove")
    @ApiOperation(value = "删除-Dictionary", notes = "删除-Dictionary")
    public void remove(@RequestBody List<String> ids) {
        dictionaryService.removeDictionary(ids);
    }

    @GetMapping("query-dict")
    @ApiOperation(value = "获取Redis中的Dictionary", notes = "获取Redis中的Dictionary")
    public Map<String, Object> queryDict() {
        return dictionaryService.queryDict();
    }

    @PostMapping("refresh")
    @ApiOperation(value = "刷新-Dictionary", notes = "刷新-Dictionary")
    public Map<String, Object> refresh(@RequestBody DictionaryDTO dto) {
        dictionaryService.initRedisDictionaries(StringUtils.defaultIfBlank(dto.getDictNameEn(), StringUtils.EMPTY));
        return dictionaryService.queryDict();
    }

}